
#include "nbt_tag.h"
#include "enbt.h"
#include "command.h"
#include <string.h>
#include <stdio.h>


#define CMD_DECL(nm) int nm (struct enbt_state *state, int argc, char **argv)

CMD_DECL(cmd_exit);
CMD_DECL(cmd_open);
CMD_DECL(cmd_write);
CMD_DECL(cmd_select);
CMD_DECL(cmd_get);
CMD_DECL(cmd_set);
CMD_DECL(cmd_new);
CMD_DECL(cmd_del);
CMD_DECL(cmd_list);
CMD_DECL(cmd_rename);
CMD_DECL(cmd_chlevel);


struct command imp_commands[] = {
	{"open",	cmd_open},
	{"write",	cmd_write},
	{"w",		cmd_write},
	{"select",	cmd_select},
	{"sel",		cmd_select},
	{"get",		cmd_get},
	{"g",		cmd_get},
	{"set",		cmd_set},
	{"s",		cmd_set},
	{"new",		cmd_new},
	{"del",		cmd_del},
	{"list",	cmd_list},
	{"ls",		cmd_list},
	{"rename",	cmd_rename},
	{"ren",		cmd_rename},
	{"cd",		cmd_chlevel},
	{"chlvl",	cmd_chlevel},
	{"exit",	cmd_exit},
	{"quit",	cmd_exit},
};

const int imp_commands_size = sizeof(imp_commands)/sizeof(struct command);


CMD_DECL(cmd_select) //state, argc, argv
{
	int i;
	
	if (argc < 2)
	{
		printf("Error: Not enough paramaters\n");
		return 1;
	}
	
	state->select = -1;
	
	if (strcmp(argv[1], "-") == 0)
	{
		return 0;
	}
	
	for(i = 0; i < state->depth_len; i++)
	{
		if (strcmp(state->depth_list[i]->name, argv[1]) == 0)
		{
			state->select = i;
		}
	}
	
	if (state->select == -1)
	{
		printf("Error: no tag with specified name\n");
		return 2;
	}
	
	return 0;
}

CMD_DECL(cmd_get)
{
	printf("Error: not implemented yet\n");
	state->is_changed = 1;
	return 1;
}

CMD_DECL(cmd_set)
{
	printf("Error: not implemented yet\n");
	state->is_changed = 1;
	return 1;
}

CMD_DECL(cmd_new)
{
	printf("Error: not implemented yet\n");
	state->is_changed = 1;
	return 1;
}

CMD_DECL(cmd_del)
{
	printf("Error: not implemented yet\n");
	state->is_changed = 1;
	return 1;
}

CMD_DECL(cmd_open)
{
	printf("Error: not implemented yet\n");
	return 1;
}

CMD_DECL(cmd_chlevel)
{
	printf("Error: not implemented yet\n");
	return 1;
}

CMD_DECL(cmd_exit)
{
	//TODO finish this
	if (state->is_changed)
	{
		printf("Do you want to save first? (Y/N) ");
		//TODO term manipulation
	}
	state->want_exit = 1;
	return 0;
}

CMD_DECL(cmd_rename)
{
	printf("Error: not implemented yet\n");
	return 1;
}

CMD_DECL(cmd_write)
{
	printf("Error: not implemented yet\n");
	return 1;
}

CMD_DECL(cmd_list)
{
	printf("Error: not implemented yet\n");
	return 1;
}

